﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;
using System.Data;

/// <summary>
/// Summary description for productBus
/// </summary>
public class productBus
{
    productDAL productDal = new productDAL();

	public productBus()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    public DataTable getProducts()
    {
        DataTable table = new DataTable();
        table=productDal.getAllProducts();
        return table;
    }
    public DataTable searchProduct(string product_name)
    {
        DataTable dt = new DataTable();
        dt = productDal.searchProduct(product_name);
        return dt;
    }
    public DataTable getProductByID(int product_id)
    {
        DataTable table = new DataTable();
        table = productDal.getProductByID(product_id);
        return table;
    }
    public product getProducstByID(int product_id)
    {
        product products=new product();
        DataTable table = new DataTable();
        table = productDal.getProductByID(product_id);
        if(!table.HasErrors){
            foreach (DataRow r in table.Rows)
            {
                products = new product(product_id,
                    r["product_name"].ToString(),
                    r["product_des"].ToString(),
                    Convert.ToInt32(r["product_quantity"].ToString()),
                    Convert.ToInt32(r["unit_id"].ToString()),
                    r["image"].ToString(),
                    Convert.ToInt32(r["manufacturer_id"].ToString()),
                    Convert.ToInt32(r["price"].ToString()),
                    Convert.ToDateTime(r["date_available"].ToString()),
                    Convert.ToBoolean(r["status"].ToString()),
                    Convert.ToDateTime(r["date_added"].ToString()),
                    Convert.ToDateTime(r["date_modified"].ToString())
                );

            }
        }
        
        return products;
    }
    public DataTable getProductRelatedByID(int product_id)
    {
        DataTable dt = new DataTable();
        dt = getProductByID(product_id);        
        int category_id =1;

        if (dt.Rows.Count > 0)
        {
            DataRow dr = dt.Rows[0];
            if (int.TryParse(dr["category_id"].ToString(), out category_id))
            {
                category_id = Convert.ToInt32(dr["category_id"].ToString());
            }
        }
       return productDal.getProductsByCategoryId(category_id);
    }
    public DataTable getProductsByCatagory_id(int category_id)
    {
        return productDal.getProductsByCategoryId(category_id);
    }

    public DataTable getProductByName(string product_name)
    {
        return productDal.getProductsByName(product_name);
    }
    public DataTable getProductByNameACategoryID(string product_name, int cateogry_id)
    {
        return productDal.getProductsByNameACategoryID(product_name, cateogry_id);
    }
    public DataTable getProductByNameACategoryIDAPrice(string product_name, int cateogry_id, int price)
    {
        return productDal.getProductsByNameACategoryIDAPrice(product_name, cateogry_id, price);
    }
    public DataTable getProductByDes(string product_des)
    {
        return productDal.getProductsByDes(product_des);
    }
    public DataTable getProductByDesACategoryID(string product_des, int category_id)
    {
        return productDal.getProductsByDesACategory(product_des, category_id);
    }
    public DataTable getProductByDesACategoryIDAPrice(string product_des, int category_id, int price)
    {
        return productDal.getProductsByDesACategoryAPrice(product_des, category_id, price);
    }

    //Insert Product
    public int insertProductBU(string product_name, string product_des, int product_quantity, int unit_id, string image, int manufacturer_id, int price, bool status)
    {
        product p = new product();
        p.Product_name = product_name;
        p.Product_des = product_des;
        p.Product_quantity = product_quantity;
        p.Unit_id = unit_id;
        p.Image = image;
        p.Manufacturer_id = manufacturer_id;
        p.Price = price;
        p.Status = status;
        int result = productDal.InsertProcductDAL(p);
        return result;
    }
    //Select All Product
    public DataTable getAllProductForAdminBU()
    {
        DataTable dt = new DataTable();
        dt = productDal.getProductForAdminDAL();
        return dt;
    }
    //Update product
    public int updateProductBU(int product_id, string product_name, string product_des, int product_quantity, int unit_id, string image, int manufacturer_id, int price, bool status, DateTime date_added, int viewed, int sort_order)
    {
        product p = new product();
        p.Product_id = product_id;
        p.Product_name = product_name;
        p.Product_des = product_des;
        p.Product_quantity = product_quantity;
        p.Unit_id = unit_id;
        p.Image = image;
        p.Manufacturer_id = manufacturer_id;
        p.Price = price;
        p.Status = status;
        p.Date_added = date_added;
        p.Viewed = viewed;
        p.Sort_Order = sort_order;
        int result = productDal.UpdateProductDAL(p);
        return result;
    }
    //Delete product

    public int DeleteProductBU(int product_id){
        product p = new product();
        p.Product_id = product_id;
        int result = productDal.deleteProduct(p);
        return result;
    }
    public void UpdateViewedByProdutID(int product_id){
        productDal.updateViewbyProductID(product_id);
    }

    public ArrayList getHotProduct(int quantity)
    {
        DataTable dtHotproduct=productDal.getHotProduct(quantity);
        ArrayList ar = new ArrayList();
        foreach (DataRow dr in dtHotproduct.Rows)
        {
            product pro = this.getProducstByID(Convert.ToInt32(dr["product_id"]));
            ar.Add(pro);
        }
        return ar;
    }

    public DataTable getBestseller(int quantity)
    {
        DataTable dtbest = productDal.getBestSeller(quantity);

        return dtbest;
    }
}